//
//  ZPTakePhotoView.m
//  NothingIsImpossible
//
//  Created by hztuen on 15/8/20.
//  Copyright (c) 2015年 hztuen. All rights reserved.
//

#import "ZPTakePhotoView.h"

@implementation ZPTakePhotoView

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@synthesize delegate;

-(void)setFrame:(CGRect)frame
{
     CGRect newFrame = CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
    [super setFrame:newFrame];
}

-(id)initWithImage:(UIImage *)image :(float)scaleYX
{
    if(self=[super init])
    {
        lastSize=image.size;
        self.backgroundColor=[UIColor blackColor];
        UIImageView *bgIV=[[UIImageView alloc]initWithFrame:self.frame];
        bgIV.backgroundColor=[UIColor blackColor];
        [self addSubview:bgIV];
        CGSize size=image.size;
        float scale1,scale=1;
        scale= SCREEN_WIDTH/size.width;
        scale1= SCREEN_WIDTH/size.width;
        if(scale>scale1)
        {
            scale=scale1;
        }
        if(scale>1)
        {
            scale=1;
        }
        lastImageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  size.width*scale, size.height*scale)];
        lastImageV.image=image;
        lastImageV.contentMode=UIViewContentModeScaleToFill;
        //    [self addSubview:lastImageV];
        
       lastScale=scale;
        
        view2=[[UIImageView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT/4,SCREEN_WIDTH, SCREEN_WIDTH*scaleYX)];
        
        [view2 addSubview:lastImageV];
        [self addSubview:view2];
        
        UIImageView *shadoeV1=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT/4)];
        shadoeV1.backgroundColor=[UIColor blackColor];
        shadoeV1.alpha=0.2;
        [self addSubview:shadoeV1];
        
        UIImageView *shadoeV2=[[UIImageView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT/4+SCREEN_WIDTH*scaleYX,SCREEN_WIDTH,SCREEN_HEIGHT-SCREEN_WIDTH*scaleYX-SCREEN_HEIGHT/4)];
        shadoeV2.backgroundColor=[UIColor blackColor];
        shadoeV2.alpha=0.2;
        [self addSubview:shadoeV2];
        
        CGRect newFrame = CGRectMake(0,SCREEN_HEIGHT-60,SCREEN_WIDTH,60);
        UIView *view=[[UIView alloc]initWithFrame:newFrame];
        [self addSubview:view];
        view.backgroundColor=[UIColor clearColor];
        
        UIImageView *shadoeV3=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 60)];
        shadoeV3.backgroundColor=[UIColor blackColor];
        shadoeV3.alpha=0.2;
        [view addSubview:shadoeV3];
        
        UIImageView *imageV2=[[UIImageView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT/4, SCREEN_WIDTH, SCREEN_WIDTH*scaleYX)];
        imageV2.layer.borderColor=[UIColor whiteColor].CGColor;
        imageV2.layer.borderWidth=1;
        [self addSubview:imageV2];
        
        UIButton* button1=[[UIButton alloc]initWithFrame:CGRectMake(20, 0, 50, 60)];
        [button1 setTitle:@"取消" forState:UIControlStateNormal];
        button1.titleLabel.font=[UIFont systemFontOfSize:15];
        [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button1];
        
        UIButton* button2=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-70,0, 50, 60)];
        [button2 setTitle:@"选取" forState:UIControlStateNormal];
        button2.titleLabel.font=[UIFont systemFontOfSize:15];
        
        [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [view addSubview:button2];
        button2.titleLabel.textColor=[UIColor whiteColor];
        [button2 addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
        UIPinchGestureRecognizer *twoMove =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(twoMove:)];
        [lastImageV addGestureRecognizer:twoMove];
        
        UIPanGestureRecognizer *oneMove =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(oneMove:)];
        [lastImageV addGestureRecognizer:twoMove];
        [lastImageV addGestureRecognizer:oneMove];
        
        lastImageV.userInteractionEnabled=YES;
        lastImageV.multipleTouchEnabled=YES;
        view2.userInteractionEnabled=YES;
    }
    return self;
}

-(void)twoMove:(UIPinchGestureRecognizer* )recognizer
{
     recognizer.scale =recognizer.scale -lastScale+1;
     lastImageV.transform=CGAffineTransformScale(lastImageV.transform, recognizer.scale,recognizer.scale);
  
    lastScale=recognizer.scale;
}

-(void)oneMove:(UIPanGestureRecognizer* )recognizer
{
    CGPoint point = [recognizer translationInView:lastImageV];
//    NSLog(@"%f,%f",point.x,point.y);
    recognizer.view.center = CGPointMake(recognizer.view.center.x + point.x, lastImageV.center.y + point.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:lastImageV];
}

-(IBAction)cancelClick:(id)sender
{
    [delegate cancelSelectImage];
    
    [self removeFromSuperview];
}

-(IBAction)sureClick:(id)sender
{
    [self ZPTakePhotoView];
    [delegate  readImage:newImage];
    [self removeFromSuperview];
}
 
-(void)ZPTakePhotoView
{
    CGRect rect1 = view2.frame;  //截取图片大小
    CGRect rect2 = lastImageV.frame;  //截取图片大小

    float lastScale1=lastImageV.frame.size.width/lastSize.width;
    
    CGRect rect = CGRectMake(-rect2.origin.x/lastScale1, -rect2.origin.y/lastScale1,rect1.size.width/lastScale1, rect1.size.height/lastScale1);//创建矩形框
 
    CGImageRef smallImage=CGImageCreateWithImageInRect([lastImageV.image CGImage], rect);
    
     newImage=[UIImage imageWithCGImage:smallImage];
    
    CGImageRelease(smallImage);
 }

@end